var sellerCode="";
$(document).ready(function() {
	//top.UppTabs.operator = top.getCurrentTagTitle();// 设置用户tab的标题值

	$( "#sellers" ).autocomplete({
		source: function( request, response ) {
	        $.ajax({
	          url: contextPath + '/iccard/report/getSellers',
	          dataType: "json",
	          data: {
	            maxRows: 10,
	            keyword: request.term
	          },
	          success: function( data ) {
	        	sellerCode="";
	            response( $.map( data, function( item ) {
	              return {
	                label: item.value+','+item.label,
	                value: item.label
	              };
	            }));
	          }
	        });
	      },
	      minLength: 2,
	      select: function( event, ui ) {
		        sellerCode=ui.item.label;
	      },
	      open: function() {
	        $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
	      },
	      close: function() {
	        $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
	      }
	    });
	
	doSearch();
});

function doSearch(){
	var cardId = $("#cardId").val();
	var cardName = $("#cardName").val();
	var s = $('input[name="beginTime"]').val();
	var e = $('input[name="endTime"]').val();
	
	$("#data_list").datagrid({
	    title:'套餐名称：'+cardName+'              起止时间：'+s+'~'+e, 
	    url:contextPath+'/iccard/report/bznis/package/detail',
		onLoadError : function() {
			uppAjaxError(data);
		},
		onLoadSuccess:function(data){
			$("#export_link").attr('href',contextPath+'/iccard/report/temp/'+data.fileName);
		},
		queryParams : {
			cardId:cardId,
			sellerCode : sellerCode,
			beginTime:s,
			endTime:e
		},
		columns : [ [ {
			field : 'combocardDailyEarningsDetailId',
			title : '',
			hidden : true
		}, {
			field : 'daliyEarningsDate',
			title : '日期',
			formatter : function(value, row, index) {
				var d =new Date(Number(value));
				return d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
			}
		}, {
			field : 'merchantName',
			title : '商家名称'
		}, {
			field : 'merchantCode',
			title : '商户编号',
			width : 80
		}, {
			field : 'comboCardExperseAmount',
			title : '套餐卡消费额'
		}, {
			field : 'comboCardHandleAmount',
			title : '套餐卡应付额'
		}, {
			field : 'comboCardExpectedEarnings',
			title : '套餐卡预期收益额'
		} ] ],
		autoRowHeight : true,
		fitColumns : true,
		pagination : true,
		nowrap : false,
		singleSelect : true,
		rownumbers : true
	});
	//
	//汇总
	$.ajax({
        url: contextPath + '/iccard/report/bznis/package/detailSum',
        dataType: "json",
        data: {
			cardId:cardId,
			sellerCode : sellerCode,
			beginTime:s,
			endTime:e
		},
        success: function( data ) {
        	$("#collect_amount").text("预期累积收益总额："+data.expectedTotal);
        }
      });

}
